const path = require("path");
const fs = require("fs");
const ejs = require("ejs");
const inquirer = require("inquirer");

const copyFiles = (name) => {
    const options = {
        componentName: name
    };
    const tmpDir = path.join(__dirname, "templates");
    const destDir = process.cwd();
    fs.readdir(tmpDir, (error, files) => {
        if (error) throw error;
        files.forEach(file => {
            ejs.renderFile(path.join(tmpDir, file), options, (error, result) => {
                if (error) throw error;
                fs.writeFileSync(path.join(destDir, file), result)
            })
        });
    })
}
const addComponent = (name) => {
    if (name) {
        copyFiles(name);
        return;
    }

    inquirer.prompt([{
        type: "input",
        name: "Component Name",
        message: "Component Name：？"
    }]).then((name) => {
        copyFiles(name);
    })

}

module.exports = addComponent;